package cn.golxen.blog.service.impl;

import cn.golxen.blog.commons.utils.LocalDateTimeUtils;
import cn.golxen.blog.commons.vo.*;
import cn.golxen.blog.entity.Essay;
import cn.golxen.blog.exception.exceptionClass.AddEssayException;
import cn.golxen.blog.mapper.EssayLabelMapper;
import cn.golxen.blog.mapper.EssayMapper;
import cn.golxen.blog.rabbitMQ.producer.Producer;
import cn.golxen.blog.redis.CategoryRedis;
import cn.golxen.blog.redis.EssayRedis;
import cn.golxen.blog.redis.LabelRedis;
import cn.golxen.blog.service.CategoryService;
import cn.golxen.blog.service.EssayService;
import cn.golxen.blog.service.LabelService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.ReactiveStreamCommands;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class EssayServiceImpl implements EssayService {

    @Autowired
    @Qualifier(value = "taskExecutor")
    ThreadPoolTaskExecutor poolTaskExecutor;

    @Autowired
    EssayMapper essayMapper;

    @Autowired
    EssayLabelMapper essayLabelMapper;

    @Autowired
    EssayRedis rEssay;

    @Autowired
    LabelRedis rLabel;

    @Autowired
    CategoryRedis rCategory;

    @Autowired
    Producer producer;


    @Override
    public int addEssay(AddEssayVo addEssayVo) {
//        try {
            Essay essay=new Essay();
            BeanUtils.copyProperties(addEssayVo,essay);
            LocalDateTime localDateTime = LocalDateTimeUtils.getLocalDateTime();
            essay.setDatetime(localDateTime);
            essay.setUpdate(localDateTime);
            essay.setEssayId(String.valueOf(System.currentTimeMillis()));
            int i=essayMapper.addEssay(essay);
            int i1 = 0;
            if(i!=0)
                i1=essayLabelMapper.addList(addEssayVo.getLabelList(),essay.getEssayId());
            poolTaskExecutor.execute(()->{
                rEssay.deleteEssay();
                rEssay.deleteEssayByCategoryAndLabel(addEssayVo.getCategory());
            });
            poolTaskExecutor.execute(()-> rLabel.deleteEssayCountByLabel());

            log.info(String.valueOf(i));
            log.info(String.valueOf(i1));
            log.info(String.valueOf(i&i1));
            if (i!=0&&i1!=0)
                return 1;
            return 0;
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new AddEssayException();
//        }
    }

    @Override
    public ResPageListVo getList(PageListVo pageListVo) {
        //redis使他不能调整当页数量
        Page<?> pageInfo=PageHelper.startPage(pageListVo.getPage(),pageListVo.getLimit());
//        String page=pageListVo.getPage().toString();
        ResPageListVo list=rEssay.getList(pageListVo,pageInfo);
        return list;
    }

    @Override
    public Integer deleteList(List<String> essayIdList) {
        Integer integer=essayMapper.deleteList(essayIdList);
        Integer integer1=0;
        if (integer!=0)
            integer1=essayLabelMapper.delete(essayIdList);
        poolTaskExecutor.execute(() -> {
            rEssay.deleteEssay(essayIdList);
            rEssay.deleteEssayByCategoryAndLabel();
//            rEssay.deleteLabelByEssay(new HashSet<>(essayIdList));
        });
        poolTaskExecutor.execute(()-> rLabel.deleteEssayCountByLabel());
        if (integer!=0&&integer1!=0)
            return 1;
        return 0;
    }

    @Override
    public Essay getEssayById(String id) {
        return rEssay.getEssayById(id);
    }

    @Override
    public Integer updateEssayById(UpdateEssayVo updateEssayVo) {
        updateEssayVo.setUpdate(LocalDateTimeUtils.getLocalDateTime());
        Integer integer=essayMapper.updateEssayById(updateEssayVo);
        Integer integer1 = 0;
        if (integer!=0)
            integer1=essayLabelMapper.update(updateEssayVo.getLabelList(), updateEssayVo.getEssayId());
        poolTaskExecutor.execute(()->{
            List<String> list=new ArrayList<>();
            list.add(updateEssayVo.getEssayId());
            rEssay.deleteEssay(list);
            rEssay.deleteEssayByCategoryAndLabel();

        });
        poolTaskExecutor.execute(()-> rLabel.deleteEssayCountByLabel());
        log.info(String.valueOf(integer));
        log.info(String.valueOf(integer1));
        if (integer!=0&&integer1!=0)
            return 1;
        return 0;
    }

    @Override
    public Integer addViewById(String id) {
        return rEssay.updateView(id);
    }

    @Override
    public Integer getCountOfCategory(String category) {
        return rEssay.getCountOfCategory(category);
    }

    @Override
    public Set<Essay> getEssaySetByCategory(String category) {
        return rEssay.getEssaySetByCategory(category);

    }


    @Override
    public Set<Essay> getEssaySetByLabel(String label) {
        return rEssay.getEssaySetByLabel(label);

    }

    @Override
    public Integer getCount() {
        return rEssay.getCount();
    }

    @Override
    public GetAllCount getAllCount() {
//        CategoryServiceImpl categoryService=new CategoryServiceImpl();
//        LabelServiceImpl labelService=new LabelServiceImpl();
        return new GetAllCount(getCount(),rCategory.getCount(),rLabel.getCount());
    }

    @Override
    public Set<String> getLabelByEssayId(String essayId) {
        return rEssay.getLabelByEssayId(essayId);
    }

    @Override
    public Set<SearchTitleVo> getEssaySetByTitle(String title) {
        return essayMapper.getEssaySetByTitle(title);
    }

    @Override
    public Set<ResLabelByEssayIdSetVo> getLabelByEssayIdSet(Set<String> essayId) {
        Set<ResLabelByEssayIdSetVo> resSet=new HashSet<>();
        Set<String> label;
        for (String id:essayId) {
            label=getLabelByEssayId(id);
            ResLabelByEssayIdSetVo resLabelVo=new ResLabelByEssayIdSetVo(id,label);
            resSet.add(resLabelVo);
        }
        return resSet;
    }

    @Override
    public List<GetDateListVo> getDateList() {
        return essayMapper.getDateList();
    }

    @Override
    public List<GetEssayListByDateVo> getEssayListByDate(String date) {
        List<GetEssayListByDateVo> list=essayMapper.getEssayListByDate(date);
        for (GetEssayListByDateVo item:list ){
            item.setDatetime(item.getDatetime().substring(0,19));
        }
        return list;

    }
}
